Imports Phoenix.DataAccess

Namespace Helpers


    Public Class MasterLevelHelper

        Public Shared Sub UpdateMasterLevel(ByVal ml As DataSetData.MasterLevel)
            AppDataLayer.UpdateMasterLevel(ml)
        End Sub

        Public Shared Sub RemoveMasterLevel(ByVal ml As DataSetData.MasterLevel)
            Dim count As Integer = 0
            For Each empl As DataAccess.DataSetData.Employee In AppDataLayer.Employees
                If empl.MasterLevelID = ml.MasterLevelID Then
                    count = count + 1
                End If
            Next
            If count > 0 Then
                AppMessageBox(String.Format(Res.MasterLevelCantDelete, count), MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation)
                Return
            Else
                If AppMessageBox(String.Format(Res.MasterLevelConfirmDelete, ml.LevelName), MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation) = MsgBoxResult.No Then
                    Return
                End If
            End If

            For Each sp As DataAccess.DataSetData.ServiceParam In ml.GetServiceParams()
                sp.Delete()
                AppDataLayer.UpdateServiceParam(sp)
            Next
            ml.Delete()
            AppDataLayer.UpdateMasterLevel(ml)
        End Sub

        Public Shared Sub CreateMasterLevel(ByVal name As String, ByVal parentMl As DataSetData.MasterLevel)
            Dim ml As DataSetData.MasterLevel = AppDataLayer.MasterLevels.NewMasterLevel
            ml.LevelName = name
            AppDataLayer.MasterLevels.AddMasterLevel(ml)
            AppDataLayer.UpdateMasterLevel(ml)
            GenerateServiceParams(ml.MasterLevelID, parentMl.MasterLevelID)
        End Sub

        Private Shared Sub GenerateServiceParams(ByVal id As Long, ByVal parentId As Long)
            Dim t As DataSetData.ServiceParamsDataTable = AppDataLayer.ServiceParams()
            For i As Integer = 0 To t.Rows.Count - 1
                Dim p As DataSetData.ServiceParam = t.Rows(i)
                If p.MasterLevelID = parentId Then
                    Dim param As DataSetData.ServiceParam = AppDataLayer.ServiceParams.NewServiceParam
                    param.ServiceID = p.ServiceID
                    param.MasterLevelID = id
                    param.Price = p.Price
                    param.Duration = p.Duration
                    AppDataLayer.ServiceParams.AddServiceParam(param)
                    AppDataLayer.UpdateServiceParam(param) 'ServiceParams.AddServiceParam(p)
                End If
            Next
        End Sub
    End Class
End Namespace

